from concurrent.futures import ThreadPoolExecutor, as_completed, wait
from functools import partial


def multithreaded(max_workers, func_name, task_list):
    executor = ThreadPoolExecutor(max_workers=max_workers)
    tasks = []
    for i in task_list:
        # 使用funools.partial函数创建一个新的函数，将参数绑定到函数中
        new_task = partial(func_name, *i)
        # executor.submit(new_task)
        tasks.append(executor.submit(new_task))
    # 处理已完成的任务
    for future in as_completed(tasks):
        try:
            result = future.result()
            # 处理任务执行的结果
            print(result)
        except Exception as e:
            # 处理任务执行出的异常
            print(f"任务{i}执行出现异常，参数为{new_task.args}")
            raise
    done, not_done = wait(tasks, return_when='ALL_COMPLETED')
    # 关闭线程池
    executor.shutdown()


def generate(month, month_days):
    res_list = []
    for i in range(1, 10):
        aa = f"{month[0:4]}-{month[4:6]}-0{i} 00:00:00"
        bb = f"{month[0:4]}-{month[4:6]}-0{i} 23:59:59"
        res_list.append((aa, bb, month))
    for i in range(10, month_days + 1):
        cc = f"{month[0:4]}-{month[4:6]}-{i} 00:00:00"
        dd = f"{month[0:4]}-{month[4:6]}-{i} 23:59:59"
        res_list.append((cc, dd, month))
    return res_list
